package com.aabte.permissionframeworklearn.permission.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * @author Daniel
 */
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T popBean(Class<T> clazz) {
        if (null == context) {
            return null;
        }
        return context.getBean(clazz);
    }

    public static <T> T popBean(String beanName, Class<T> clazz) {
        if (null == context) {
            return null;
        }
        return context.getBean(beanName, clazz);
    }

    public static <T> Map<String, T> popBeanMap(Class<T> clazz) {
        if (null == context) {
            return null;
        }
        return context.getBeansOfType(clazz);
    }

}
